<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="10" :lg="11" :md="12" :sm="24">
            <a-form-item :label="$t('业务时间')">
              <j-date
                :show-time="{ defaultValue: moment('00:00:00', 'HH:mm:ss') }"
                valueFormat="YYYY-MM-DD HH:mm:ss"
                dateFormat="YYYY-MM-DD HH:mm:ss"
                :placeholder="$t('请选择开始时间')"
                class="query-group-cust"
                v-model="queryParam.createTime_begin"
              ></j-date>
              <span class="query-group-split-cust"></span>
              <j-date
                :show-time="{ defaultValue: moment('23:59:59', 'HH:mm:ss') }"
                valueFormat="YYYY-MM-DD HH:mm:ss"
                dateFormat="YYYY-MM-DD HH:mm:ss"
                :placeholder="$t('请选择结束时间')"
                class="query-group-cust"
                v-model="queryParam.createTime_end"
              ></j-date>
            </a-form-item>
          </a-col>
          <a-col :xl="5" :lg="6" :md="7" :sm="24">
            <a-form-item :label="$t('支付方式')">
              <a-select v-model="queryParam.payType">
                <a-select-option v-for="(item, index) in paytypeList" :key="index" :value="item.name">{{
                  item.nameText
                }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>

          <template v-if="toggleSearchStatus">
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item :label="$t('支付单号')">
                <a-input :placeholder="$t('请输入支付单号')" v-model="queryParam.thirdOrderId"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item :label="$t('收款渠道')">
                <a-select v-model="queryParam.recoveryChannel">
                  <a-select-option v-for="(item, index) in paychannelList" :value="item.value" :key="index">{{
                    item.title
                  }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item :label="$t('支付状态')">
                <a-select v-model="queryParam.status">
                  <a-select-option v-for="(item, index) in paystateList" :value="item.value" :key="index">{{
                    item.title
                  }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item :label="$t('订单号')">
                <a-input :placeholder="$t('请输入订单号')" v-model="queryParam.orderId"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item :label="$t('商户订单号')">
                <a-input :placeholder="$t('请输入商户订单号')" v-model="queryParam.outTradeNo"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24" v-if="!tenantid">
              <a-form-item :label="$t('门店')">
                <a-select v-model="queryParam.shopCode">
                  <a-select-option v-for="(item, i) in shops" :key="i" :value="item.value"
                    >{{ item.title }}<span v-if="item.value != ''">（{{ item.value }}）</span></a-select-option
                  >
                </a-select>
              </a-form-item>
            </a-col>
          </template>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">{{ $t('查询') }}</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">{{
                $t('重置')
              }}</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? $t('收起') : $t('展开') }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a>
              <a-button
              v-has="'BisOrderPayController:export'"
                v-if="toggleSearchStatus"
                type="primary"
                style="margin-left: 8px"
                icon="download"
                :loading="exloading"
                @click="handleExportXls($t('支付订单'))"
                >{{ $t('导出') }}</a-button
              >
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- table区域-begin -->
    <div>
      <a-table
        ref="table"
        size="middle"
        :scroll="{ x: 1670, y: tableHeight }"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :customHeaderRow="customHeaderRow"
        :customRow="customRow"
        class="j-table-force-nowrap"
        @change="handleTableChange"
      >
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px; font-style: italic">{{ $t('无图片') }}</span>
          <img
            v-else
            :src="getImgView(text)"
            height="25px"
            alt=""
            style="max-width: 80px; font-size: 12px; font-style: italic"
          />
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px; font-style: italic">{{ $t('无文件') }}</span>
          <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="downloadFile(text)">
            {{ $t('下载') }}
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click.stop="showModal(record)">{{ $t('详情') }}</a>
        </span>

        <template slot="footer">
          <div style="font-weight: bold">
            <span style="margin-left: 15px; margin-right: 5px">{{ $t('业务笔数') }}：</span>
            <span>{{ totalInfo.count }}</span>
            <span style="margin-left: 15px; margin-right: 5px">{{ $t('订单金额') }}：</span>
            <span>{{ totalInfo && totalInfo.price ? totalInfo.price.toFixed(2) : '' }}</span>
            <span style="margin-left: 15px; margin-right: 5px" v-if="taxSetting!=0">{{ $t('其中税费') }}：</span>
            <span v-if="taxSetting!=0">{{ totalInfo && totalInfo.taxPrice ? totalInfo.taxPrice.toFixed(2) : '' }}</span>
            <span style="margin-left: 15px; margin-right: 5px">{{ $t('退款金额') }}：</span>
            <span>{{ totalInfo && totalInfo.refundFee ? totalInfo.refundFee.toFixed(2) : '' }}</span>
          </div>
        </template>
      </a-table>
    </div>

    <order-pay-modal ref="modalForm"></order-pay-modal>
  </a-card>
</template>

<script>
import Vue from 'vue'
import moment from 'moment'
import '@/assets/less/TableExpand.less'
import { getAction } from '@/api/manage'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { filterMultiDictText, initDictOptions } from '@/components/dict/JDictSelectUtil'
import OrderPayModal from './modules/OrderPayModal'

export default {
  name: 'OrderPayList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    OrderPayModal,
  },
  data() {
    return {
      description: '支付订单管理页面',
      moment,
      tenantid: Vue.ls.get('TENANT_ID'),
      // 表头
      columns: [
        {
          title: this.$t('订单号'),
          align: 'center',
          dataIndex: 'orderId',
          width: 240,
        },
        {
          title: this.$t('下单时间'),
          align: 'center',
          dataIndex: 'createTime',
          width: 240,
          customCell: (r, i) => {
            return {
              style: {
                whiteSpace: 'normal',
              },
            }
          },
        },
        {
          title: this.$t('商户订单号'),
          align: 'center',
          dataIndex: 'outTradeNo',
          width: 240,
          customCell: (r, i) => {
            return {
              style: {
                whiteSpace: 'normal',
              },
            }
          },
        },
        {
          title: this.$t('支付单号'),
          align: 'center',
          dataIndex: 'thirdOrderId',
          width: 280,
          customCell: (r, i) => {
            return {
              style: {
                whiteSpace: 'normal',
              },
            }
          },
        },
        {
          title: this.$t('收款渠道'),
          align: 'center',
          dataIndex: 'recoveryChannel_dictText',
          width: 110,
          customCell: (r, i) => {
            return {
              style: {
                whiteSpace: 'normal',
              },
            }
          },
        },
        {
          title: this.$t('支付方式'),
          align: 'center',
          dataIndex: 'payType_dictText',
          width: 140,
          customCell: (r, i) => {
            return {
              style: {
                whiteSpace: 'normal',
              },
            }
          },
        },
        {
          title: this.$t('支付状态'),
          align: 'center',
          dataIndex: 'status_dictText',
          width: 100,
          customCell: (r, i) => {
            return {
              style: {
                whiteSpace: 'normal',
              },
            }
          },
        },
        {
          title: this.$t('订单金额'),
          align: 'center',
          sorter: true,
          dataIndex: 'price',
          customRender: (text, record) => {
            return record.price.toFixed(2)
          },
          width: 100,
          customCell: (r, i) => {
            return {
              style: {
                whiteSpace: 'normal',
                textAlign: 'right',
              },
            }
          },
        },
        {
          title: this.$t('退款金额'),
          align: 'center',
          sorter: true,
          dataIndex: 'refundFee',
          customRender: (text, record) => {
            if (record.refundFee || record.refundFee === 0) {
              return record.refundFee.toFixed(2)
            }
            return ''
          },
          width: 140,
          customCell: (r, i) => {
            return {
              style: {
                whiteSpace: 'normal',
                textAlign: 'right',
              },
            }
          },
        },
        {
          title: this.$t('操作'),
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 80,
          customCell: (r, i) => {
            return {
              style: {
                whiteSpace: 'normal',
              },
            }
          },
          scopedSlots: { customRender: 'action' },
        },
      ],
      url: {
        list: '/pay/bisOrderPay/list',
        delete: '/pay/bisOrderPay/delete',
        exportXlsUrl: '/pay/bisOrderPay/exportXls',
        totalUrl: 'pay/bisOrderPay/total',
      },
      dictOptions: {},
      paytypeList: [],
      paychannelList: [],
      paystateList: [],
      queryParam: {
        payType: '',
        recoveryChannel: '',
        status: '',
        shopCode: '',
      },
      info: {},
      shops: [],
      isfooter: true,
      isCustom: true,
      isHideData: false,
      hideCond: 2,
      taxSetting: localStorage.getItem('taxSetting')
    }
  },
  created() {
    if (!this.tenantid) {
      let dtl = {
          title: this.$t('门店编码'),
          align: 'center',
          dataIndex: 'shopCode',
          width: 100,
          customCell: (r, i) => {
            return {
              style: {
                whiteSpace: 'normal',
              },
            }
          },
        },
        dtl1 = {
          title: this.$t('门店名称'),
          align: 'center',
          dataIndex: 'shopCode_dictText',
          width: 140,
          customCell: (r, i) => {
            return {
              style: {
                whiteSpace: 'normal',
              },
            }
          },
        }
      this.columns.unshift(dtl1)
      this.columns.unshift(dtl)
    }else{
      this.taxSetting = localStorage.getItem("taxSetting")
      if(this.taxSetting != 0){
        let dtl0 = {
            title: this.$t('其中税费'),
            align: 'center',
            dataIndex: 'taxPrice',
            width: 100,
            customRender: (text, record) => {
              return record.taxPrice.toFixed(2)
            },
            customCell: (r, i) => {
              return {
                style: {
                  whiteSpace: 'normal',
                  textAlign: 'right'
                },
              }
            },
          }
        this.columns.splice(this.columns.length - 2, 0, dtl0)
      }
    }
  },
  mounted() {
    let that = this
    document.onkeydown = function (e) {
      let key = window.event.keyCode
      if (key == 75 && event.ctrlKey) {
        window.event.preventDefault()
        that.isHideData = false
        that.queryParam.dataType = 0
        that.searchQuery()
      }
    }
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },
  },
  methods: {
    initDictConfig() {
      this.customInit()
      let param = {}
      param.pageNo = 1
      param.pageSize = 9999
      param.orderType = 0
      getAction('/pay/bisPayType/list', param).then((res) => {
        if (res.success) {
          res.result.records.forEach((col) => {
            if (col.state == 'Y') {
              this.paytypeList.push(col)
            }
          })
          this.paytypeList.unshift({ name: '', nameText: this.$t('全部') })
        }
      })
      initDictOptions('bis_pay_channel.name').then((res) => {
        if (res.success) {
          this.paychannelList = res.result
          this.paychannelList.unshift({ value: '', title: this.$t('全部') })
        }
      })
      initDictOptions('bis_order_pay.state').then((res) => {
        if (res.success) {
          this.paystateList = res.result
          this.paystateList.unshift({ value: '', title: this.$t('全部') })
        }
      })
      initDictOptions('bis_shop,name,code').then((res) => {
        if (res.success) {
          this.shops = res.result
          this.shops.unshift({ value: '', title: this.$t('全部') })
        }
      })
    },
    customInit() {
      this.isHideData = true
      this.queryParam.dataType = 2
      this.loadData(1)
    },
    searchReset() {
      this.queryParam = {
        createTime_begin: '',
        createTime_end: '',
        payType: '',
        thirdOrderId: '',
        recoveryChannel: '',
        status: '',
        orderId: '',
        outTradeNo: '',
        shopCode: '',
      }
      this.selectedRowIndex = []
      this.customInit()
    },
    showModal(record) {
      this.$refs.modalForm.edit(record)
    },
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>